<template>
	<view class="bg min100 pb-20">
		<!-- <u-sticky h5-nav-height="0">
			<view class="status_bar bg-blue">
			</view>
			<view class="bg-blue white">
				<view class="center pd-20" @click="getCity1">
					<view class="size-34 mr-10">{{city}}</view>
					<u-icon name="arrow-down"></u-icon>
				</view>
				
			</view>
		</u-sticky> -->
		<!-- 	<u-tabs v-if="tabList.length>1" :list="tabList" font-size="32" bg-color="#120062" height="90" active-color="#fff"
		 inactive-color="#eee" :is-scroll="false" :current="index" @change="change"></u-tabs> -->
		 <view class="cu-custom" :style="{height: $wanlshop.wanlsys().height + 'px' }">
		 	<view class="cu-bar fixed" :style="{
		 		height: $wanlshop.wanlsys().height + 'px', 
		 		paddingTop: $wanlshop.wanlsys().top + 'px',
		 		color: '#333333',
		 		backgroundColor: '#fff',
		 	}">
		 		<view class="action" @tap="$wanlshop.on('/pages/index')">
		 			<text class="wlIcon-fanhui1"></text>
		 		</view>
		 		<view class="content text-bold" :style="{top: $wanlshop.wanlsys().top + 'px'}" @click="getCity1">
		 			<view class="size-34 mr-10">{{city}}</view>
		 			<u-icon name="arrow-down"></u-icon>
		 		</view>
		 		<view class="action" @tap="$wanlshop.on('/pages/index')">
		 			<text class="iconfont icon-ipr_shouye text-bold" style="font-size: 44rpx;"></text>
		 		</view>
		 	</view>
		 </view>
		<view class="pd-30">
			<wanl-empty v-if="list.length==0" text="暂无订单"></wanl-empty>
			<view v-for="(item,i) in list" :key="i" class="mb-30">
				<orderItem v-if="current==0" :type="1" :item="item"></orderItem>
				<sfItem v-if="current==1" type="1" :item="item"></sfItem>
				<hyItem v-if="current==2" type="1" :item="item"></hyItem>
			</view>
			<u-loadmore v-show="list.length>9" :status="status" icon-type="flower" bg-color="transperant"
				margin-top="30" margin-bottom="30" />
			<image @click="toPublish" type="1" v-if="current==1" src="/static/img/sf-pub.png"
				style="width: 100rpx;height: 100rpx;position: fixed;bottom: 120rpx;right: 20rpx;" mode=""></image>
		</view>
		<u-tabbar v-model="currentTab" :list="listTab" active-color="#120062" @change="jump"></u-tabbar>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import orderItem from '@/components/order-item.vue'
	import sfItem from '@/components/sf-item.vue'
	import hyItem from '@/components/huoyun/hy-item.vue'
	export default {
		computed: {
			...mapState(['tabList'])
		},
		data() {
			return {
				index: 1,
				current: '',
				currentTab: 0, // 底部
				listTab: [{
						"pagePath": "/pages/driver/home/index",
						"iconPath": "/static/img/tabbar/1.png",
						"selectedIconPath": "/static/img/tabbar/1-1.png",
						"text": "首页"
					}, {
						"pagePath": "/pages/driver/order/index",
						"iconPath": "/static/img/tabbar/2.png",
						"selectedIconPath": "/static/img/tabbar/2-2.png",
						"text": "订单"
					}, {
						"pagePath": "/pages/driver/message/index",
						"iconPath": "/static/img/tabbar/5.png",
						"selectedIconPath": "/static/img/tabbar/5-1.png",
						"text": "消息"
					},
					{
						"pagePath": "/pages/driver/my/index",
						"iconPath": "/static/img/tabbar/3.png",
						"selectedIconPath": "/static/img/tabbar/3-3.png",
						"text": "我的"
					}
				], // 底部
				page: 1,
				status: 'loadmore',
				list: [],
				timer1: null,
				timer2: null,
				timer3: null,
				flag: false,
				city:''
			}
		},
		onShow() {
			this.flag = false;
			this.page = 1
			this.list = []
			this.status = 'loadmore'
			this.timer3 = setInterval(() => {
				if (this.tabList && this.tabList[0]) {
					clearInterval(this.timer3)
					this.timer3 = null
					if (!this.current) {
						this.current = this.tabList[0].id
					}
					this.city = uni.getStorageSync('city');
					// 列表接口
					if (this.city) {
						console.log(666);
						if (this.current == 0) {
							this.getList1()
						} else if (this.current == 1) {
							this.getList2()
						} else if (this.current == 2) {
							this.getList3()
						}
					} else {
						let _this = this
						// #ifdef H5

						if (_this.current == 0) {
							_this.getList1()
						} else if (_this.current == 1) {
							_this.getList2()
						} else if (_this.current == 2) {
							_this.getList3()
						}

						// #endif
						// #ifdef APP-PLUS
						uni.getLocation({
							type: 'wgs84',
							geocode: true,
							success: mres => {
								console.log("mresmresmresmres", mres)
								let longitude = mres.longitude;
								let latitude = mres.latitude;
								uni.setStorageSync('wanlshop:lng', longitude);
								uni.setStorageSync('wanlshop:lat', latitude);

								var point = new plus.maps.Point(mres.longitude, mres.latitude);
								//静态方法，反向地理编码
								plus.maps.Map.reverseGeocode(point, {}, (event) => {
									var address = event.address; // 转换后的地理位置
									var point = event.coord; // 转换后的坐标信息
									var coordType = event.coordType; // 转换后的坐标系类型
									// var reg = /.+?(省|市|自治区|自治州|区|县)/g;
									var reg = /.+?(省|市)/g;
									var addressList = address.match(reg).toString().split(",");
									console.log("cxzczczxczc", addressList)
									_this.$store.commit('getCity', addressList[1])

									if (_this.current == 0) {
										_this.getList1()
									} else if (_this.current == 1) {
										_this.getList2()
									} else if (_this.current == 2) {
										_this.getList3()
									}
								}, function(e) {
									console.log("失败回调", e);
								});
							},
							fail: error => {
								// console.log("失败回调",error);
							}
						});
						// #endif
						// uni.getLocation({
						// 	type: 'wgs84',
						// 	geocode: true,
						// 	success(res) {
						// 		let city = res.address ? res.address.city : '合肥市'
						// 		_this.$store.commit('getCity', city)
						// 		if (_this.current == 0) {
						// 			_this.getList1()
						// 		} else if (_this.current == 1) {
						// 			_this.getList2()
						// 		}else if (_this.current == 2) {
						// 			_this.getList3()
						// 		}
						// 	},
						// 	fail() {
						// 		if (_this.current == 0) {
						// 			_this.getList1()
						// 		} else if (_this.current == 1) {
						// 			_this.getList2()
						// 		}else if (_this.current == 2) {
						// 			_this.getList3()
						// 		}
						// 	}
						// });
					}
					// 实时更新新下的单
					if (!this.timer1) {
						this.timer1 = setInterval(() => {
							if (this.current == 0 && this.flag) {
								this.$http('/addons/ddrive/order/order_refresh', {
									order_id: this.list.length > 0 ? this.list[0].id : '',
									city: this.city || ''
								}, "POST").then(data => {
									if (data.length > 0) {
										data.forEach(item => {
											this.list.unshift(item)
										})
									}
								})
							} else if (this.current == 1 && this.flag) {
								this.$http('/addons/ddrive/sforder/order_refresh', {
									order_type: 1,
									order_id: this.list.length > 0 ? this.list[0].id : '',
									city: this.city || ''
								}, "POST").then(data => {
									if (data.length > 0) {
										data.forEach(item => {
											this.list.unshift(item)
										})
									}
								})
							} else if (this.current == 2 && this.flag) {
								this.$http('/addons/ddrive/hyorder/order_refresh', {
									order_id: this.list.length > 0 ? this.list[0].id : '',
									city: this.city || ''
								}, "POST").then(data => {
									if (data.length > 0) {
										data.forEach(item => {
											this.list.unshift(item)
										})
									}
								})
							}
						}, 1000)
					}
					// 实时更新减少的单
					if (!this.timer2) {
						this.timer2 = setInterval(() => {
							if (this.list.length > 0 && this.current == 0) {
								this.$http('/addons/ddrive/order/order_eliminate', {
									city: this.city || ''
								}, "POST").then(data => {
									if (data.length > 0) {
										this.list.forEach((item, i) => {
											if (data.includes(item.id * 1)) {
												this.list.splice(i, 1)
											}
										})
									}
								})
							} else if (this.list.length > 0 && this.current == 1) {
								this.$http('/addons/ddrive/sforder/order_eliminate', {
									order_type: 1,
									city: this.city || ''
								}, "POST").then(data => {
									if (data.length > 0) {
										this.list.forEach((item, i) => {
											if (data.includes(item.id * 1)) {
												this.list.splice(i, 1)
											}
										})
									}
								})
							} else if (this.list.length > 0 && this.current == 2) {
								this.$http('/addons/ddrive/hyorder/order_eliminate', {
									city: this.city || ''
								}, "POST").then(data => {
									if (data.length > 0) {
										this.list.forEach((item, i) => {
											if (data.includes(item.id * 1)) {
												this.list.splice(i, 1)
											}
										})
									}
								})
							}
						}, 5000)
					}
				}
			}, 200)
		},
		onUnload() {
			clearInterval(this.timer1)
			clearInterval(this.timer2)
			clearInterval(this.timer3)
			this.timer1 = null
			this.timer2 = null
			this.timer3 = null
		},
		onHide() {
			clearInterval(this.timer1)
			clearInterval(this.timer2)
			clearInterval(this.timer3)
			this.timer1 = null
			this.timer2 = null
			this.timer3 = null
		},
		onPullDownRefresh() {
			this.page = 1
			this.flag = false
			this.list = []
			if (this.current == 0) {
				this.getList1()
			} else if (this.current == 1) {
				this.getList2()
			} else if (this.current == 2) {
				this.getList3()
			}
		},
		onReachBottom() {
			//避免多次触发
			if (this.status == 'loading' || this.status == 'nomore') {
				return;
			}
			if (this.current == 0) {
				this.getList1()
			} else if (this.current == 1) {
				this.getList2()
			} else if (this.current == 2) {
				this.getList3()
			}
		},
		components: {},
		methods: {
			jump(e) {
				console.log(e)
				uni.navigateTo({
					url: this.listTab[e].pagePath
				})
			},
			//代驾
			getList1() {
				this.flag = false
				this.status = "loading";
				this.$http('/addons/ddrive/order/takingList', {
					page: this.page,
					city: this.city
				}, "POST").then(res => {
					let data = res.data
					if (data.length < 10) {
						this.status = "nomore"
					} else {
						this.page = this.page + 1
						this.status = "loadmore"
					}
					this.list = this.list.concat(data)
					this.flag = true
					uni.stopPullDownRefresh();
				})
			},
			//顺风车
			getList2() {
				this.flag = false
				this.status = "loading";
				this.$http('/addons/ddrive/sforder/order_index', {
					page: this.page,
					start_city: this.city,
					order_type: 1
				}, "POST").then(res => {
					let data = res
					if (data.length < 10) {
						this.status = "nomore"
					} else {
						this.page = this.page + 1
						this.status = "loadmore"
					}
					this.list = this.list.concat(data)
					this.flag = true
					uni.stopPullDownRefresh();
				})
			},
			//货运
			getList3() {
				this.flag = false
				this.status = "loading";
				this.$http('/addons/ddrive/hyorder/taskingList', {
					page: this.page,
					city: this.city,
				}, "POST").then(res => {
					let data = res.data
					if (data.length < 10) {
						this.status = "nomore"
					} else {
						this.page = this.page + 1
						this.status = "loadmore"
					}
					this.list = this.list.concat(data)
					this.flag = true
					uni.stopPullDownRefresh();
				})
			},
			change(index) {
				this.index = index;
				this.current = this.tabList[index].id;
				this.page = 1;
				this.flag = false;
				this.list = [];
				if (this.current == 0) {
					this.getList1()
				} else if (this.current == 1) {
					this.getList2()
				} else if (this.current == 2) {
					this.getList3()
				}
			},
			getCity1() {
				uni.navigateTo({
					url: '/pages/driver/home/city'
				})
			},
			// 顺风车发布
			toPublish() {
				uni.navigateTo({
					url: '/pages/driver/shunfeng/publish'
				})
			}
		}
	}
</script>

<style scoped lang="scss">

</style>